package donot996.icu.tieba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import donot996.icu.tieba.domain.TopicList;
import donot996.icu.tieba.mapper.TopicListMapper;
import donot996.icu.tieba.service.ITopicListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TopicListServiceImpl extends ServiceImpl<TopicListMapper,TopicList> implements ITopicListService {
    @Autowired
    TopicListMapper topicListMapper;

    @Override
    public List<TopicList> getListInfo() {
        QueryWrapper<TopicList> topicListQueryWrapper = new QueryWrapper<>();
        List<TopicList> topicLists = topicListMapper.selectList(topicListQueryWrapper);
        for (TopicList topicList : topicLists) {
            String detailIds = topicList.getDetailIds();
            String[] ids = detailIds.split(",");
            topicList.setNum("".equals(ids[0]) ? 0L : (long) ids.length); // 获取回复数(注意字符串的比较)
             // 获取发起人姓名
            topicList.setDetailIds(null);
        }
        return  topicLists;
    }

    @Override
    public void submitTopic(Long userId, String userName, String content) {
        TopicList topicList = new TopicList();
        topicList.setTopicName(content);
        topicList.setDetailIds("");
        topicList.setInitiatorId(userId);
        topicList.setInitiatorName(userName);
        topicListMapper.insert(topicList);
    }
}
